package android.kryten.service.impl;

import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.kryten.model.Note;
import android.kryten.service.NoteService;
import android.util.Log;

public class NoteServiceImpl implements NoteService{

	private static final String TAG = "NOTE_SERVICE";
	private static String URL_ADD_TOPIC_NOTE = "opensocial/ui/topics/notes";
	
	public HttpClient client;
	
	public NoteServiceImpl(){
		super();
		this.client = new DefaultHttpClient();
	}
	
	public List<Note> getAllNotes(Long topicId) {
		
		// /opensocial/ui/topics/notes/?id={id_topic}
		
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * TOTO NEFUNGUJE .. asi na strane open social, treba opravit, nebo se zeptat paji jesi je v google docs spravna url
	 */
	public void addNote(Long topicId,String note) {
				
		try {
						
			HttpPost req = new HttpPost(URLEncoder.encode("http://10.0.2.2:8080/" + URL_ADD_TOPIC_NOTE + "/?id=" + topicId.toString() +"&note=" + note,"utf-8"));
	          
			
	        HttpResponse resp = client.execute(req);
	        if (resp.getStatusLine().getStatusCode() != 200) {
	            Log.e(TAG, "Bad server response code:"+resp.getStatusLine().getStatusCode());
	            //there we can return null, or throw an exception, because nothing better cannot be done            
	        }
	        
			} catch (Exception e) {
	            Log.e(TAG, "Unable to add topic note", e);	            
	        }
		
	}

}
